"""
進入爱逛卖家版
"""
import time

import yaml
from uiautomator2 import xpath

from page_object.add_market_page import AddGoodsPage
from page_object.broadcast_create_page import CreateBroadcastPage
from page_object.broadcast_promotion_page import BroadCastPromotionPage
from page_object.client_living_page import ClientLivingPage
from page_object.client_server_page import ClienServerPage
from page_object.guang_page import GuangPage
from page_object.living_page import LivingPage
from page_object.message_page import MessagePage
from page_object.order_page import OrderPage
from page_object.personal_page import PersonalPage
from page_object.preview_create_page import PreviewCreatePage
from page_object.preview_manager_page import PreviewManagerPage
from page_object.shopwin_page import ShopWinPage
from page_object.source_config_page import SourceConfigPage
from resource.client_path_info import *
from resource.path_info import *
from util.basic_util import check_living_status

"""
爱逛卖家首页
"""
class GuangMainPage(GuangPage):

    def intoPersonalPage(self, d):
        print("intoPersonalPage")
        d.xpath(personal_xpath).wait(timeout=2.0)
        if d.xpath(personal_xpath).exists:
            d.xpath(personal_xpath).click()
        return PersonalPage()

    def returnPersonalPage(self, d):
        print("intoPersonalPage")
        return PersonalPage()

    def get_user_name(self,d):
        return d.xpath(userName_xpath).get_text()

    def intoShopWinPage(self, d):
        print("intoShopWindowsPage")
        d.xpath(toShopWin_xpath).wait(timeout=5.0)
        d.xpath(toShopWin_xpath).click()
        time.sleep(3)
        return ShopWinPage()

    def returnShopWinPage(self, d):
        return ShopWinPage()

    def intoMarketPage(self, d):
        print("intoMarketPage")
        d.xpath(toMarket_xpath).wait(timeout=5.0)
        d.xpath(toMarket_xpath).click()
        return AddGoodsPage()

    def returnMarketPage(self, d):
        print("returnMarketPage")
        return AddGoodsPage()
    '''
    进入商品来源配置页
    '''
    def intoSourceConfigPage(self, d):
        print("intoSourceConfigPage")
        d.xpath(toSourceConfig_xpath).wait(timeout=5.0)
        d.xpath(toSourceConfig_xpath).click()
        return SourceConfigPage()

    '''
    进入直播预告创建页
    '''
    def intoPreviewCreatePage(self, d):
        print("intoPreviewCreatePage")
        d.xpath(toPreviewCreate_xpath).wait(timeout=5.0)
        d.xpath(toPreviewCreate_xpath).click()
        return PreviewCreatePage()

    '''
    进入直播预告配置页
    '''

    def intoPreviewManagerPage(self, d):
        print("intoPreviewManagerPage")
        d.xpath(toPreviewManager_xpath).wait(timeout=5.0)
        d.xpath(toPreviewManager_xpath).click()
        return PreviewManagerPage()

    def returnPreviewManagerPage(self, d):
        print("returnPreviewManagerPage")
        return PreviewManagerPage()

    '''
    进入直播推广页
    '''

    def intoBroadCastPromotionPage(self, d):
        print("intoBroadCastPromotionPage")
        d.xpath(toBroadCastPromotion_xpath).wait(timeout=5.0)
        d.xpath(toBroadCastPromotion_xpath).click()
        return BroadCastPromotionPage()

    def returnBroadCastPromotionPage(self, d):
        print("returnBroadCastPromotionPage")
        return BroadCastPromotionPage()

    '''
    进入立即直播
    '''

    def intoCreateBroadcastPage(self, d):
        print("intoCreateBroadcastPage")
        d.xpath(toCreateBroadcast_xpath).wait(timeout=5.0)
        d.xpath(toCreateBroadcast_xpath).click()
        return CreateBroadcastPage()

    def returnCreateBroadcastPage(self, d):
        print("returnCreateBroadcastPage")
        return CreateBroadcastPage()

    '''
    进入直播间
    '''

    def returnLivingPage(self, d):
        print("returnLivingPage")
        d.xpath(living_to_goods_bag_xpath).wait(timeout=2)
        for i in range(1,4,1):
            if not d.xpath(living_to_goods_bag_xpath).exists:
                d.press('back')
                time.sleep(1)
        return LivingPage()

    def intoGuangClient(self, d):
        print("intoGuangClient")
        d.app_stop("com.guang.client")
        d.app_start("com.guang.client")
        self.checkIntoGuangClient(d)
        return ClientLivingPage()

    def intoGuang(self, d):
        print("intoGuang")
        d.app_stop("com.youzan.guang.business")
        d.app_start("com.youzan.guang.business")
        if d.xpath(main_agree_button_xpath).exists:
            d.xpath(main_agree_button_xpath).click()
        d.xpath(main_ad_xpath).wait(timeout=2.0)
        if d.xpath(main_ad_xpath).exists:
            d.xpath(main_ad_xpath).click()
        # privacy agreement
        d.xpath(privacy_dialog_agree_xpath).wait(timeout=3.0)
        if d.xpath(privacy_dialog_agree_xpath).exists:
            d.xpath(privacy_dialog_agree_xpath).click()
        # check login status
        d.xpath(userName_xpath).wait(timeout=10.0)
        if d.xpath(userName_xpath).exists:
            print("enter guang business successfully!")
            check_living_status(self, d)
        return LivingPage()

    def returnGuang(self, d):
        print("returnGuang")
        d.app_start("com.youzan.guang.business")
        return LivingPage()

    def checkIntoGuangClient(self, d):
        d.xpath(client_main_follow_xpath).wait(timeout=5.0)
        if d.xpath(client_main_follow_xpath).exists:
            print("已进入爱逛C端APP")
        else:
            time.sleep(3)
            if not d.xpath(client_main_follow_xpath).exists:
                print("进入爱逛C端APP失败")
                raise Exception("进入爱逛C端APP失败")

    '''
    进入订单管理
    '''

    def intoOrderPage(self, d):
        print("intoOrderPage")
        d.xpath(toOrder_xpath).click()
        return OrderPage()

    def returnOrderPage(self, d):
        print("returnOrderPage")
        return OrderPage()

    '''
    进入消息中心ClienServerPage
    '''

    def intoMessageManager(self, d):
        print("intoMessageManager")
        d.xpath(toMessage_xpath).click()
        return MessagePage()

    def returnMessageManager(self, d):
        print("returnMessageManager")
        return MessagePage()

    '''
    进入客服
    '''

    def intoClientServer(self, d):
        print("intoClientServer")
        time.sleep(3)
        d.swipe(0.516, 0.642,0.463, 0.264,0.2)
        if not d.xpath(toClientServer_xpath).exists:
            d.swipe(0.516, 0.642, 0.463, 0.264, 0.2)
        d.xpath(toClientServer_xpath).click()
        return ClienServerPage()

    def returnClientServer(self, d):
        print("returnClientServer")
        return ClienServerPage()